import CryptoJS from 'crypto-js'
const iv = "Ch7juDLTLwZgQNvq"
// 加密参数
export function aesEncrypt(key, word) {
    try {
        // Encrypt
        let str = JSON.stringify(word);
        let pkey = CryptoJS.enc.Utf8.parse(key);
        let encrypted = CryptoJS.AES.encrypt(str, pkey, {
            iv: CryptoJS.enc.Utf8.parse(iv),
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        encrypted = encrypted.toString()
        return encrypted;
    } catch (e) {
        console.error(e);
    }
    return null;
}

// 解密参数
export function aesDecrypt(key, data) {
    try {
        let bytes  = CryptoJS.AES.decrypt(data, CryptoJS.enc.Utf8.parse(key),{
            iv: CryptoJS.enc.Utf8.parse(iv),
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        let decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
        return decryptedData
    } catch (e) {
        console.log(e, "decrypt 的 err值为");
        return data;
    }
}
export function createNonceStr (range) {
    let str = "",
        arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
    for (let i = 0; i < range; i++) {
        let pos = Math.round(Math.random() * (arr.length - 1));
        str += arr[pos];
    }
    return str;
}
